// 确保 character_training.pending_exp 字段存在；不存在则自动新增
const mysql = require('mysql2/promise');
const config = require('./database-config.js');

async function main() {
    const conn = await mysql.createConnection(config.source);
    try {
        const dbName = config.source.database || config.source.db || config.source.databaseName;
        if (!dbName) throw new Error('未找到数据库名');

        const [rows] = await conn.execute(
            `SELECT COLUMN_NAME FROM information_schema.COLUMNS
       WHERE TABLE_SCHEMA=? AND TABLE_NAME='character_training' AND COLUMN_NAME='pending_exp'`,
            [dbName]
        );

        if (rows && rows.length > 0) {
            console.log('✅ pending_exp 字段已存在');
            return;
        }

        console.log('🔧 新增字段 pending_exp...');
        await conn.execute(
            `ALTER TABLE character_training
       ADD COLUMN pending_exp BIGINT NOT NULL DEFAULT 0 COMMENT '本次训练可领取的经验（1207记录，1208领取）'`
        );
        console.log('✅ 新增完成');
    } catch (e) {
        console.error('❌ 执行失败:', e.message || e);
        process.exitCode = 1;
    } finally {
        await conn.end();
    }
}

if (require.main === module) {
    main();
}




